package mundomensajero.main;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

public class DestinatariosView extends Activity {
	ListView lv;
    ArrayList<DestinatarioData> data = new ArrayList<DestinatarioData>();
    ArrayList<String> destinatariosAux = new ArrayList<String>();
    int checkeados[] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
    int TIGO=R.drawable.tigo_logo;
    int CLARO=R.drawable.claro_logo;
    int DIGICEL=R.drawable.digicel_logo;
    int MOVISTAR=R.drawable.movistar_logo;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_viewdestinatarios);
        lv = (ListView)findViewById(R.id.LstOpciones);
        
        sacarData();

        AdaptadorDestinatarios adaptador = new AdaptadorDestinatarios(this,data.size());
        lv.setAdapter(adaptador);
    }   
    
    private void sacarData(){
    	Bundle bundle=getIntent().getExtras();
        int i,cant = bundle.getInt("numDestinatarios");
        ArrayList<String> dest;
        boolean estado;
        for(i=0; i<cant; i++){
    		dest = new ArrayList<String>();
    		dest = bundle.getStringArrayList(i+"");
    		if(dest.get(2).equals("true")) estado=true;
    		else estado=false;
    		data.add(new DestinatarioData(dest.get(0), dest.get(1),estado));
    		destinatariosAux.add(dest.get(1));
    		checkeados[i]=1;
    	}
    }
   
    public void msj(String s){
    	Toast.makeText(this,s,Toast.LENGTH_SHORT).show();
    }
    
//    private OnCheckedChangeListener mStarCheckedChanceChangeListener = new OnCheckedChangeListener() {
//        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//        	int i;
//        	for(i=0; i<data.size(); i++){
//        		if(buttonView.getText().toString().contains(destinatariosAux.get(i))) break;
//        	}
//        	if(isChecked)
//        		checkeados[i]=1;
//        	else{
//        		msj("El mensaje no se enviará a "+buttonView.getText().toString().split("\n")[0].toString());
//        		checkeados[i]=0;
//        	}
//        }
//    };
    
    public void onClickRegresar(View Button){
    	Intent principal = new Intent();
		principal.setClass(this,MundoMensajero_Activity.class);
		principal.putExtra("checkeados", checkeados);
		setResult(RESULT_OK, principal);
		finish();
    }
    
//    @SuppressWarnings("rawtypes")
//	class AdaptadorDestinatarios extends ArrayAdapter {
//    	Activity context;
//    	@SuppressWarnings("unchecked")
//    	AdaptadorDestinatarios(Activity context) {
//    		super(context, R.layout.listcheck, data);
//    		this.context = context;
//    	}    	
//    	public View getView(int position, View convertView, ViewGroup parent) {
//    		
//    		
//    		
//			LayoutInflater inflater = context.getLayoutInflater();
//			View item = inflater.inflate(R.layout.listcheck, null);
//
//			CheckBox cb = (CheckBox)item.findViewById(R.id.check);
//			if(data.get(position).getNombre().toString().length()==0)
//				cb.setText(data.get(position).getNumero().toString());
//			else
//				cb.setText(data.get(position).getNombre().toString()+"\n"+data.get(position).getNumero().toString());
//			cb.setChecked(data.get(position).isChecked());
//			cb.setOnCheckedChangeListener(mStarCheckedChanceChangeListener);
//			return(item);
//		}
//    }
    
    private class AdaptadorDestinatarios extends BaseAdapter {
    	private boolean[] itemSelection;
    	Activity context;

    	AdaptadorDestinatarios(Activity context,int size) {
    		super();
    		this.context = context;
    		this.itemSelection = new boolean[size];
    		for(int i=0; i<size; i++){
    			itemSelection[i]=true;
    		}
    	}
    	
    	public int getCount() {
    		return data.size();
    	}
    	 
    	public String getItem(int position) {
    		return data.get(position).toString();
    	}
    	 
    	public long getItemId(int position) {
    		return position;
    	}
    	 
    	public View getView(final int position, View convertView, ViewGroup parent) {
    		LayoutInflater inflater = context.getLayoutInflater();
    		View item = inflater.inflate(R.layout.listcheck, null);
    		
    		final CheckBox cb = (CheckBox)item.findViewById(R.id.check);
    		ImageView iv = (ImageView)item.findViewById(R.id.companylogo);
    		iv.setImageResource(identificarCompania(data.get(position).getNumero().toString()));
    		
    		if(data.get(position).getNombre().toString().length()==0)
    			cb.setText(data.get(position).getNumero().toString());
			else
				cb.setText(data.get(position).getNombre().toString()+"\n"+data.get(position).getNumero().toString());
    		cb.setChecked(data.get(position).enviarmsj());
    		cb.setOnCheckedChangeListener(new OnCheckedChangeListener(){
    			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    				itemSelection[position] = cb.isChecked();
    				int i;
    	        	for(i=0; i<data.size(); i++){
    	        		if(buttonView.getText().toString().contains(destinatariosAux.get(i))) break;
    	        	}
    	        	if(isChecked)
    	        		checkeados[i]=1;
    	        	else{
    	        		msj("El mensaje no se enviará a "+buttonView.getText().toString().split("\n")[0].toString());
    	        		checkeados[i]=0;
    	        	}
    			}
    		});
    		
    		cb.setChecked(itemSelection[position]);
    		return item;
    	}
    	 
    	}
    	 
    	public static class ViewHolder {
    		public CheckBox chkItem;
    	}
    	 
    	
    	@Override
        public boolean onKeyDown(int keyCode, KeyEvent event){   
        	
        	if(keyCode == KeyEvent.KEYCODE_BACK){
        		onClickRegresar(null);
        		return true;
        	}
    		return super.onKeyDown(keyCode, event);
        }
    	
    	public int identificarCompania(String number){	    	
    	   	int n;
			String num = number.substring(0,4);
			n = Integer.parseInt(num);
			
			if(((n>=7000)&&(n<=7099))||		     
			     ((n>=7600)&&(n<=7669))||
			     ((n>=7687)&&(n<=7690))||
			     ((n>=7740)&&(n<=7759))||
			     ((n>=7805)&&(n<=7809))||
			     ((n>=7840)&&(n<=7844))||
			     ((n>=7850)&&(n<=7869))||
			     ((n>=7950)&&(n<=7969))||
			     ((n>=7985)&&(n<=7989))){
					return CLARO;		
			}
								
			if(((n>=7300)&&(n<=7399))||		    			   
				((n>=7400)&&(n<=7449))||			   						   
				((n>=7460)&&(n<=7469))||
				((n>=7695)&&(n<=7698))||
				((n>=7760)&&(n<=7779))||
				((n>=7970)&&(n<=7979))){
				 return DIGICEL;			 
			}
		
			if(((n>=7100)&&(n<=7199))||
				  ((n>=7450)&&(n<=7459))||
				  ((n>=7691)&&(n<=7694))||
			      ((n>=7700)&&(n<=7719))||
			      ((n>=7780)&&(n<=7784))||
			      ((n>=7790)&&(n<=7794))||
			      ((n>=7810)&&(n<=7839))||
			      ((n>=7845)&&(n<=7849))||
			      ((n>=7990)&&(n<=7999))){
				return MOVISTAR;
			}
			
			if(((n>=7200)&&(n<=7299))||
			     ((n>=7470)&&(n<=7499))||
			     ((n>=7500)&&(n<=7599))||			     
			     ((n>=7670)&&(n<=7679))||
			     ((n>=7680)&&(n<=7683))||
			     ((n>=7720)&&(n<=7739))||
			     ((n>=7785)&&(n<=7789))||
			     ((n>=7795)&&(n<=7799))||
			     ((n>=7870)&&(n<=7899))||
			     ((n>=7900)&&(n<=7949))){
					return TIGO;

				}
			return -1;		    	
    	}
    
    
       
}
